<template>
  <div class="app-container">
    <el-card>
      <el-row :gutter="20" align="middle">
        <el-col :span="24" class="input-button-group">
          <el-input v-model="name" placeholder="输入名称" clearable style="width: 200px;" />
          <el-button icon="el-icon-search" @click="handleSearch">筛选</el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      height="532"
      border
      fit
      highlight-current-row
    >
      <!-- ID Column (Read-only) -->
      <el-table-column align="center" sortable :sort-by="sortById" label="ID" width="100">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>

      <!-- Editable Name Column -->
      <el-table-column label="小区名称" width="220" align="center">
        <template slot-scope="scope">
          <template v-if="isEditing(scope.row.id)">
            <el-input v-model="scope.row.name" size="small" />
          </template>
          <template v-else>
            {{ scope.row.name }}
          </template>
        </template>
      </el-table-column>

      <!-- Editable Address Column -->
      <el-table-column label="地址" width="220" align="center">
        <template slot-scope="scope">
          <template v-if="isEditing(scope.row.id)">
            <el-input v-model="scope.row.address" size="small" />
          </template>
          <template v-else>
            <span>{{ scope.row.address }}</span>
          </template>
        </template>
      </el-table-column>

      <!-- Editable Manager Column -->
      <el-table-column label="管理员" width="220" align="center">
        <template slot-scope="scope">
          <template v-if="isEditing(scope.row.id)">
            <el-input v-model="scope.row.manager" size="small" />
          </template>
          <template v-else>
            {{ scope.row.manager }}
          </template>
        </template>
      </el-table-column>

      <!-- Editable Phone Column -->
      <el-table-column label="电话" width="220" align="center">
        <template slot-scope="scope">
          <template v-if="isEditing(scope.row.id)">
            <el-input v-model="scope.row.phone" size="small" />
          </template>
          <template v-else>
            {{ scope.row.phone }}
          </template>
        </template>
      </el-table-column>

      <!-- Actions Column -->
      <el-table-column label="操作" width="257" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="toggleEdit(scope.row.id)"
          >{{ isEditing(scope.row.id) ? '保存' : '编辑' }}</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="block" style="text-align: center;">
      <el-pagination
        :current-page="page"
        :page-sizes="[9, 18, 27]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getList, updateRow, deleteRow } from '@/api/community'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      page: 1,
      pageSize: 9,
      name: '',
      total: 0,
      editingRowId: null
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList({ page: this.page, pageSize: this.pageSize, name: this.name }).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    sortById(a, b) {
      return a.id - b.id
    },
    handleSearch() {
      this.page = 1
      this.fetchData()
    },
    handleSizeChange(size) {
      this.pageSize = size
      this.fetchData()
    },
    handleCurrentChange(page) {
      this.page = page
      this.fetchData()
    },
    toggleEdit(rowId) {
      if (this.editingRowId === rowId) {
        this.saveChanges(rowId)
        this.editingRowId = null
      } else {
        this.editingRowId = rowId
      }
    },
    isEditing(rowId) {
      return this.editingRowId === rowId
    },
    saveChanges(rowId) {
      const updatedRow = this.list.find(row => row.id === rowId)
      updateRow(updatedRow).then(() => {
        this.$message.success('修改成功')
        this.fetchData()
      })
    },
    handleDelete(rowId) {
      const ids = [rowId]
      this.$confirm('确定删除该记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRow(ids).then(() => {
          this.fetchData()
          this.$message.success('删除成功')
        })
      }).catch(() => {
        this.$message('已取消删除')
      })
    }
  }
}
</script>

<style scoped>
.input-button-group {
  display: flex;
  align-items: center;
}

.input-button-group .el-input {
  flex: 1;
  margin-right: 10px;
}

.app-table {
  width: 100%;
}
</style>
